package cn.zn.modules.money.cur.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.zn.framework.common.exception.RRException;
import cn.zn.framework.common.utils.FileUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zn.modules.money.cur.dao.TMoneyInfoDetailAttachMapper;
import cn.zn.modules.money.cur.entity.TMoneyInfoDetailAttach;
import cn.zn.modules.money.cur.service.TMoneyInfoDetailAttachService;
import org.springframework.web.multipart.MultipartFile;

@Service
public class TMoneyInfoDetailAttachServiceImpl extends ServiceImpl<TMoneyInfoDetailAttachMapper, TMoneyInfoDetailAttach> implements TMoneyInfoDetailAttachService {

    @Autowired
    private FileUtils fileUtils;

    @Override
    public void saveAttach(String detailId, MultipartFile[] fileupload) {
        try {
            LocalDate now = LocalDate.now();
            Collection<TMoneyInfoDetailAttach> attaches = CollUtil.newArrayList();
            String fileName = "";
            TMoneyInfoDetailAttach tMoneyInfoDetailAttach;
            File outputFile;
            for (MultipartFile multipartFile : fileupload) {
                fileName = multipartFile.getOriginalFilename();
                outputFile = fileUtils.updateFile(multipartFile.getInputStream(), fileName.substring(fileName.lastIndexOf(".") + 1), now.toString().split("-"));
                tMoneyInfoDetailAttach = new TMoneyInfoDetailAttach();
                tMoneyInfoDetailAttach.setTMoneyInfoDetailId(detailId);
                tMoneyInfoDetailAttach.setContentType(multipartFile.getContentType());
                tMoneyInfoDetailAttach.setGenerateName(outputFile.getName().substring(0, outputFile.getName().lastIndexOf(".")));
                tMoneyInfoDetailAttach.setPathLocal(outputFile.getPath());
                tMoneyInfoDetailAttach.setPathRelative(fileUtils.getNoBasePath(outputFile.getPath()).replace(File.separator, "/"));
                tMoneyInfoDetailAttach.setRealName(fileName.substring(0, fileName.lastIndexOf(".")));
                tMoneyInfoDetailAttach.setSize(multipartFile.getSize());
                tMoneyInfoDetailAttach.setSuffix(fileName.substring(fileName.lastIndexOf(".") + 1));
                tMoneyInfoDetailAttach.setIsMain(false);
                attaches.add(tMoneyInfoDetailAttach);
            }
            super.saveBatch(attaches);
        } catch (IOException e) {
            log.error("", e);
            throw new RRException("");
        }
    }

    @Override
    public void deleteAttach(List<String> ids) {
        List<TMoneyInfoDetailAttach> attaches = baseMapper.selectBatchIds(ids);
        attaches.forEach(o -> {
            FileUtil.del(o.getPathLocal());
            baseMapper.deleteById(o.getId());
        });
    }
}
